package com.yisu.es;

import org.apache.http.HttpHost;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class InsertTest {
    public static void main(String[] args) {
        RestHighLevelClient client = new RestHighLevelClient(
                RestClient.builder(new HttpHost("localhost", 9200, "http")));

        try {
            // 示例文档1
            Map<String, Object> doc1 = new HashMap<>();
            doc1.put("user_id", "1");
            doc1.put("role_id", 1);
            doc1.put("role_code", "admin");
            doc1.put("mobile", "13800138000");
            doc1.put("job_number", "JN001");
            doc1.put("user_name", "张三");
            doc1.put("dept_group_id", 1);
            doc1.put("dept_supplier_id", 1);
            doc1.put("user_type", "sys_user");
            doc1.put("sex", "1");
            doc1.put("ext_number", "101");
            doc1.put("ivr_agent_no", "IVR001");
            doc1.put("ivr_agent_name", "张三");
            doc1.put("genesys_agent_no", "GEN001");
            doc1.put("seats_idle_state", "working");
            doc1.put("seats_login_state", "login");
            doc1.put("avatar", "http://example.com/avatar1.jpg");
            doc1.put("password", "hashed_password1");
            doc1.put("pwd_salt", "salt1");
            doc1.put("status", 1);
            doc1.put("del_flag", 1);
            doc1.put("login_ip", "192.168.1.1");
            doc1.put("login_date", "2023-10-01T12:00:00Z");
            doc1.put("max_call_am", 50);
            doc1.put("max_call_pm", 50);
            doc1.put("day_allocation", 100);
            doc1.put("names_number", 5);
            doc1.put("remark", "备注信息1");
            doc1.put("create_by", "admin");
            doc1.put("created_time", "2023-10-01T10:00:00Z");
            doc1.put("update_by", "admin");
            doc1.put("updated_time", "2023-10-01T12:00:00Z");
            doc1.put("tel_ext_number", "101");
            doc1.put("group_type", "销售组");
            doc1.put("sso_user_id", 1L);
            doc1.put("signin_time", "2023-10-01T09:00:00Z");
            doc1.put("rta_flag", 1);

            IndexRequest request1 = new IndexRequest("tel_user").id("1").source(doc1);
            IndexResponse response1 = client.index(request1, RequestOptions.DEFAULT);
            System.out.println(response1.getResult());

            // 示例文档2
            Map<String, Object> doc2 = new HashMap<>();
            doc2.put("user_id", "2");
            doc2.put("role_id", 2);
            doc2.put("role_code", "user");
            doc2.put("mobile", "13900139000");
            doc2.put("job_number", "JN002");
            doc2.put("user_name", "李四");
            doc2.put("dept_group_id", 2);
            doc2.put("dept_supplier_id", 2);
            doc2.put("user_type", "sys_user");
            doc2.put("sex", "2");
            doc2.put("ext_number", "102");
            doc2.put("ivr_agent_no", "IVR002");
            doc2.put("ivr_agent_name", "李四");
            doc2.put("genesys_agent_no", "GEN002");
            doc2.put("seats_idle_state", "rest");
            doc2.put("seats_login_state", "logout");
            doc2.put("avatar", "http://example.com/avatar2.jpg");
            doc2.put("password", "hashed_password2");
            doc2.put("pwd_salt", "salt2");
            doc2.put("status", 1);
            doc2.put("del_flag", 1);
            doc2.put("login_ip", "192.168.1.2");
            doc2.put("login_date", "2023-10-02T12:00:00Z");
            doc2.put("max_call_am", 40);
            doc2.put("max_call_pm", 40);
            doc2.put("day_allocation", 80);
            doc2.put("names_number", 3);
            doc2.put("remark", "备注信息2");
            doc2.put("create_by", "admin");
            doc2.put("created_time", "2023-10-02T10:00:00Z");
            doc2.put("update_by", "admin");
            doc2.put("updated_time", "2023-10-02T12:00:00Z");
            doc2.put("tel_ext_number", "102");
            doc2.put("group_type", "客服组");
            doc2.put("sso_user_id", 2L);
            doc2.put("signin_time", "2023-10-02T09:00:00Z");
            doc2.put("rta_flag", 0);

            IndexRequest request2 = new IndexRequest("tel_user").id("2").source(doc2);
            IndexResponse response2 = client.index(request2, RequestOptions.DEFAULT);
            System.out.println(response2.getResult());

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                client.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
